(function() {

    var support = { animations : Modernizr.cssanimations },
        container = document.getElementById( 'ip-container' ),
        header = container.querySelector( 'header.ip-header' ),
        loader = new PathLoader( document.getElementById( 'ip-loader-circle' ) ),
        animEndEventNames = { 'WebkitAnimation' : 'webkitAnimationEnd', 'OAnimation' : 'oAnimationEnd', 'msAnimation' : 'MSAnimationEnd', 'animation' : 'animationend' },
        // animation end event name
        animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ];

    function init() {
        var onEndInitialAnimation = function() {
            if( support.animations ) {
                this.removeEventListener( animEndEventName, onEndInitialAnimation );
            }

            startLoading();
        };

        // disable scrolling
        window.addEventListener( 'scroll', noscroll );

        // initial animation
        classie.add( container, 'loading' );

        if( support.animations ) {
            container.addEventListener( animEndEventName, onEndInitialAnimation );
        }
        else {
            onEndInitialAnimation();
        }
    }


    function loadScript(urls, callback, charset){
        if(typeof(urls) != "object") var urls = [urls];
        var HEAD = document.getElementsByTagName("head").item(0) || document.documentElement, s = new Array(), loaded = 0;
          for(var i=0; i<urls.length; i++) {
                s[i] = document.createElement("script");
                s[i].type = "text/javascript";
                if(charset){
                    s[i].setAttribute("charset",charset);
                }
                if (s[i].readyState){
                    s[i].onreadystatechange = function(){
                        if(s[i].readyState == "loaded" || s[i].readyState == "complete"){
                            s[i].onreadystatechange = null;
                            callback(i/urls.length);

                            //this.parentNode.removeChild(this);
                        }
                    };
                }
                else {
                    s[i].onload = function(){
                        callback(i/urls.length);
                        //this.parentNode.removeChild(this);
                    };
                }
                s[i].src = urls[i]+"?v="+Math.random();
                document.getElementsByTagName("head")[0].appendChild(s[i]);
            }
        }


    function startLoading() {
        // simulate loading something..
        var Fn = function(instance) {
            var scripts = ["js/jquery-2.1.1.min.js","js/svgLoader.js","js/svgLoader.js","js/fullscreenForm.js","js/imagesloaded.pkgd.min.js","js/masonry.pkgd.min.js","js/cbpGridGallery.js","js/cbpSplitLayout.js","js/jquery.mobile.custom.min.js","js/jquery.mCustomScrollbar.concat.min.js","js/page/main.js"];
            loadScript(scripts,function(progress) {
                    progress = Math.min( progress, 1 );

                    instance.setProgress( progress );

                    // reached the end
                    if( progress === 1 ) {
                        classie.remove( container, 'loading' );
                        classie.add( container, 'loaded' );
                        

                        var onEndHeaderAnimation = function(ev) {
                            if( support.animations ) {
                                if( ev.target !== header ) return;
                                this.removeEventListener( animEndEventName, onEndHeaderAnimation );
                            }

                            classie.add( document.body, 'layout-switch' );
                            window.removeEventListener( 'scroll', noscroll );
                        };

                        if( support.animations ) {
                            header.addEventListener( animEndEventName, onEndHeaderAnimation );
                        }
                        else {
                            onEndHeaderAnimation();
                        }
                    }
                });
               
        };

        loader.setProgressFn( Fn );
    }
    
    function noscroll() {
        window.scrollTo( 0, 0 );
    }

    init();

})();